package servlets;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Hashtable;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import common.Configuration;

@WebServlet(name="SaveQuest", urlPatterns={"/JSP/Maha/SaveQuest"})
public class SaveQuest extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Statement stmt = null;
	Statement stmt1 = null;
    ResultSet rst = null;
    Statement stmt2 = null;
    ResultSet rst2 = null;
    Statement stmt3 = null;
    ResultSet rst3 = null;
    Statement stmt4 = null;
    ResultSet rst4 = null;
    Statement stmt5 = null;
    ResultSet rst5 = null;
    Statement stmt6 = null;
    public SaveQuest() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub	// TODO Auto-generated method stub
		String titre=request.getParameter("titre");
		int nbrQL=Integer.parseInt((String)request.getParameter("nbrQL"));
		int nbrQM=Integer.parseInt((String)request.getParameter("nbrQM"));
		int nbrQU=Integer.parseInt((String)request.getParameter("nbrQU"));
		String questid =(String)request.getParameter("questid");
        //get QL
		HashMap QL=new HashMap();
		for(int i=1;i<=nbrQL;i++){
			QL.put(i,(String)request.getParameter("QL" + i));
	    }
		//
		// get QM 
		Hashtable   QMLibelle=new Hashtable();
		Hashtable   QMPos=new Hashtable();

		for(int j=0;j<nbrQM;j++){
			String sj=j+"";
			QMLibelle.put(sj,request.getParameter("QM"+j));
			//nbre de possibilites
			int     nbrpos=0;
			nbrpos=Integer.valueOf((String)request.getParameter("pos"+j)).intValue(); 
			for (int t=0;t<nbrpos;t++)
			{
				String st=j+""+t;  
				QMPos.put(st,request.getParameter("QM" + j + "res" + t));
			}
		}
		//
		// Get QU
		Hashtable   QULibelle=new Hashtable();
		Hashtable   QUPos=new Hashtable();
		
		for(int j=0;j<nbrQU;j++){
			String sj=j+"";
			QULibelle.put(sj,request.getParameter("QU"+j));
			//nbre de possibilites
			int  nbrposU = 0;
			nbrposU = Integer.valueOf((String)request.getParameter("posU"+j)).intValue(); 
			for (int t=0;t<nbrposU;t++)
			{
				String st=j+""+t;  
				QUPos.put(st,request.getParameter("QU" + j + "res" + t));
			}
		}
		
		 request.setAttribute("titre", titre);
		 request.setAttribute("nbrQL", nbrQL);
		 request.setAttribute("QL", QL);
		 request.setAttribute("nbrQM", nbrQM);
		 request.setAttribute("QMLibelle", QMLibelle);
		 request.setAttribute("QMPos", QMPos);
		 request.setAttribute("nbrQU", nbrQU);
	     request.setAttribute("QULibelle", QULibelle);
	     request.setAttribute("QUPos", QUPos);
		 stmt = Configuration.GetStatement();
		 stmt1 = Configuration.GetStatement();
		 stmt2 = Configuration.GetStatement();
		 stmt3 = Configuration.GetStatement();
		 stmt4 = Configuration.GetStatement();
		 stmt5 = Configuration.GetStatement();
		 stmt6 = Configuration.GetStatement();
		 int count = 0;
		 try          { 
			 
			 if(questid == null ) {
				  // save Question Title
				  rst = stmt.executeQuery("select * from  questiongroup order by idquestiongroup desc limit 1;");
				  while( rst.next() ) {
					    count = rst.getInt( "idquestiongroup" );
				  }
				 count = count + 1 ;
				 int  query1 = stmt.executeUpdate (" insert into questiongroup values ('" + count + "','" + titre + "','')");
				 // Save Questions Libres
				 int qstcount = 0;
				 rst = stmt.executeQuery("Select * from question order by idquestion desc limit 1;");
				  while( rst.next() ) {
					  qstcount = rst.getInt( "idquestion" )  ;
				  }
				  int subqstcount = 0;
				  rst = stmt.executeQuery("Select * from subquestion order by idsubquestion desc limit 1;");
				  while( rst.next() ) {
					  subqstcount = rst.getInt( "idsubquestion" )  ;
				  }
				 for(int i=1;i<=nbrQL;i++){ 
					 qstcount = qstcount + 1;
					 String qstdesc = (String)request.getParameter("QL" + i);
					 int query2 = stmt.executeUpdate (" insert into question values ('" + qstcount + "','" + titre + "','" + qstdesc +
							                          "','" + count + "','1')");
				 }
				// Save Questions Multiples
				 for(int i=0;i<nbrQM;i++){ 
					 qstcount = qstcount + 1;
					 String qstdesc = (String)request.getParameter("QM" + i);
					 int     nbrpos=0;
					 nbrpos=Integer.valueOf((String)request.getParameter("pos"+i)).intValue(); 
					 int query2 = stmt.executeUpdate (" insert into question values ('" + qstcount + "','" + titre + "','" + qstdesc +
							                          "','" + count + "','2')");
					 for (int t=0;t<nbrpos;t++){
						 subqstcount = subqstcount + 1;
						 String subqstdesc = (String)request.getParameter("QM" + i + "res" + t);
						 int query3 = stmt.executeUpdate (" insert into subquestion values ('" + subqstcount + "','" + titre + "','" + subqstdesc +
		                          "','" + qstcount + "')");
						 
					 }
				 }
				// Save Questions Uniques
				 for(int i=0;i<nbrQU;i++){ 
					 qstcount = qstcount + 1;
					 String qstdesc = (String)request.getParameter("QU" + i);
					 int     nbrpos=0;
					 nbrpos=Integer.valueOf((String)request.getParameter("posU"+i)).intValue();
					 int query3 = stmt.executeUpdate (" insert into question values ('" + qstcount + "','" + titre + "','" + qstdesc +
							                          "','" + count + "','3')");
					 
					 for (int t=0;t<nbrpos;t++){
						 subqstcount = subqstcount + 1;
						 String subqstdesc = (String)request.getParameter("QU" + i + "res" + t);
						 int query4 = stmt.executeUpdate (" insert into subquestion values ('" + subqstcount + "','" + titre + "','" + subqstdesc +
		                          "','" + qstcount + "')");
						 
					 }
				 }
				 
				 
			 }
			 else {
			  // save Question Title
		      int query1 = stmt.executeUpdate (" UPDATE questiongroup set QGTitle = '" +  titre + "' where idQuestionGroup = " + questid );
		      // Save Questions Libres
		      rst = stmt1.executeQuery("Select * from question where idQuestionGroup = " + questid + " and idQuestionType = 1" );
		     int i = 0;
		     while( rst.next() ) {
			     i = i + 1;
				 String qstdesc = (String)request.getParameter("libre" + i);
			     int query2 = stmt2.executeUpdate ("UPDATE question SET QuestionTitle = '" + titre + "', QuestionDesc = '" +  qstdesc +
			       "', idQuestionGroup = '" + questid + "', idQuestionType = 1" + " where idquestion = " + rst.getInt( "idquestion" )); 
			    }
			  
			  //Save Questions Multiples
			  rst = stmt.executeQuery("Select * from question where idQuestionGroup = '" + questid + "' and idQuestionType = '2'" );
			  int j = 0; 
			  while( rst.next()) {
					 String qstdesc = (String)request.getParameter("QM" + j);
					  int query3 = stmt2.executeUpdate ("UPDATE question SET QuestionTitle = '" + titre + "', QuestionDesc = '" +  qstdesc +
						       "', idQuestionGroup = '" + questid + "', idQuestionType = '2'" + " where idquestion = " + rst.getInt( "idquestion" ));
					rst2 = stmt2.executeQuery("Select * from subquestion where idQuestion = '" + rst.getInt( "idquestion" ) + "'");
					int k = 0; 
					while( rst2.next()) {
					  String subqstdesc = (String)request.getParameter("QM" + j + "res" + k );
					  int query4 = stmt3.executeUpdate ("UPDATE subquestion Set SubQuestionTitle = '" + titre + "', " +
					           "SubQuestionDesc = '" + subqstdesc + "' where idQuestion = '" + rst2.getInt( "idquestion" ) + "' " +
					           		"and idSubQuestion = '" + rst2.getInt( "idSubquestion" ) + "'");
					  k = k + 1;
						 
					 }
					j = j + 1;
			  }
			  
			 //Save Questions Uniques
			  rst3 = stmt.executeQuery("Select * from question where idQuestionGroup = '" + questid + "' and idQuestionType = '3'" );
			  int x = 0; 
			  while( rst3.next()) {
					 String qstdesc = (String)request.getParameter("QU" + x);
					  int query3 = stmt4.executeUpdate ("UPDATE question SET QuestionTitle = '" + titre + "', QuestionDesc = '" +  qstdesc +
						       "', idQuestionGroup = '" + questid + "', idQuestionType = '3'" + " where idquestion = " + rst3.getInt( "idquestion" ));
					rst4 = stmt5.executeQuery("Select * from subquestion where idQuestion = '" + rst3.getInt( "idquestion" ) + "'");
					int y = 0; 
					while( rst4.next()) {
					  String subqstdesc = (String)request.getParameter("QU" + x + "res" + y);
					  int query4 = stmt6.executeUpdate ("UPDATE subquestion Set SubQuestionTitle = '" + titre + "', " +
					           "SubQuestionDesc = '" + subqstdesc + "' where idQuestion = '" + rst4.getInt( "idquestion" ) + "'" +
					           		"and idSubQuestion = '" + rst4.getInt( "idSubquestion" ) + "'");
					  y = y + 1;
						 
					 }
					x = x + 1 ;
				 }
			  
			 }	 
			   
	
			 
			  
			 
		 }
		 catch( Exception e )           { 
		     System.out.println( "Exception Error:  " + e );       
		  }  
		RequestDispatcher view = request.getRequestDispatcher("SaveQuest.jsp");
		view.forward(request, response); 
	}

}
